global class PopulateBatchedUsers{
    WebService static String populateRemoteAddress(String URL)
    {
        Remote_IP_Address__c[] rip = [select IPAddress__c from Remote_IP_Address__c];
        if(rip.size() > 0)
        {
            rip[0].IPAddress__c = URL;
        }
        else
        {
            rip = new Remote_IP_Address__c[1];   
            rip[0] = new Remote_IP_Address__c(IPAddress__c = URL);
        }
        try
        {
            upsert rip;
        }
        catch(Exception e)
        {
            return e.getMessage();
        }
        return 'success';
    }//end of populateRemoteAddress()
    
WebService static String populateObject(String[] UserIdList, String[] RoleIdList, String templateId, String bId){
   String result = '';
   BatchedUser__c bu = null;
   Batch__c bch = null;
   
   String batchId = bId;
   
   
   System.LoggingLevel level = LoggingLevel.DEBUG;
   
   if(bId != 'null')
   {
    BatchedUser__c[] busr = [SELECT Id from BatchedUser__c b where b.Batchid__c = :bId];
    for(BatchedUser__c b: busr)
        delete b;
   }
   else
   {
      bch = new Batch__c();
      insert bch;
      batchId = bch.Id;
   }

   Integer userSize = UserIdList.size();

   for(Integer i=0; i< userSize; i++)
   {
      result += UserIdList[i]+'  _  '+RoleIdList[i]+'\n';
      bu = new BatchedUser__c();
      bu.TemplateId__c = templateId;
      bu.UserId__c = UserIdList[i];
      bu.UserRoleId__c = RoleIdList[i];
      bu.Batchid__c = batchId;
      insert bu;
   }
   return batchId;
}

WebService static String populateObject2(String[] UserIdList, String[] TitleList, String templateId, String bId){
   String result = '';
   BatchedUser__c bu = null;
   Batch__c bch = null;
   
   String batchId = bId;
   
   
   System.LoggingLevel level = LoggingLevel.DEBUG;
   
   if(bId != 'null')
   {
    BatchedUser__c[] busr = [SELECT Id from BatchedUser__c b where b.Batchid__c = :bId];
    for(BatchedUser__c b: busr)
        delete b;
   }
   else
   {
      bch = new Batch__c();
      insert bch;
      batchId = bch.Id;
   }

   Integer userSize = UserIdList.size();

   for(Integer i=0; i< userSize; i++)
   {
      result += UserIdList[i]+'  _  '+TitleList[i]+'\n';
      bu = new BatchedUser__c();
      bu.TemplateId__c = templateId;
      bu.UserId__c = UserIdList[i];
      bu.User_Title__c = TitleList[i];
      bu.Batchid__c = batchId;
      insert bu;
   }
   return batchId;
}


WebService static String[] getSelectedUsers(String batchId, String templateId)
{
   BatchedUser__c[] bUser = [SELECT UserId__c, UserRoleId__c, TemplateId__c, User_Title__c from BatchedUser__c b where b.Batchid__c = :batchId];// and b.TemplateId__c = :templateId];
   
   String[] bUserArray = new String[bUser.size()];

   Integer i = 0;
   if(bUser.size() > 0)
   {
       for(BatchedUser__c bU: bUser)
       {
            User[] user = null;
            if(bu != null)
                user = [SELECT FirstName, LastName, UserRole.Name, Title FROM User u where u.Id = :bU.UserId__c  and (u.UserRole.Id = :bU.UserRoleId__c or u.Title = :bU.User_Title__c)];
            if(user.size() > 0)
            {
                String userDetail = user[0].FirstName+' '+user[0].LastName+'__'+user[0].UserRole.Name+'__'+user[0].Title;
        
                bUserArray[i] = userDetail;
            
                i++;
            }
       }
    }

   return bUserArray;
   
}

    WebService static String[] getBatchDetails(String templateId){
       Batch__c[] batch = [SELECT Id, Name, Status__c, StartTime__c, EndTime__c, TotalContracts__c from Batch__c b where b.TemplateId__c = :templateId and b.Status__c != 'INProgress'];
       String[] batchList = new String[batch.size()];
       Integer i = 0;
       for(Batch__c b: batch)
       {
           String batchDetail = b.Id+'__'+b.Name+'__'+b.Status__c+'__'+b.StartTime__c.format('MM/dd/yyyy hh:mm:ss a')+'__'+((b.EndTime__c == null)? '' : b.EndTime__c.format('MM/dd/yyyy hh:mm:ss a'))+'__'+b.TotalContracts__c;
           batchList[i] = batchDetail;
           i++;
       }
       return batchList;
    }
    
    //Method invoked from 'retreiveTemplates' function of 'BatchDet_AftGen_SCon' Scontrol
    WebService static String getStartEndDateTime(String batchId)
    {
        Batch__c[] batch = [Select StartTime__c, EndTime__c from Batch__c b where b.Id = :batchId];
        if(batch.size() >0)
            return batch[0].StartTime__c.format('MM/dd/yyyy hh:mm:ss a')+'__'+((batch[0].EndTime__c == null)? '' : batch[0].EndTime__c.format('MM/dd/yyyy hh:mm:ss a'));

            
        return null;
    }
    
    WebService static String deleteBatch(String batchId)
    {
        for(List<Sales_Contract__c> sc : [Select Id from Sales_Contract__c sc where sc.BatchID__c = :batchId])
        {
           try
           {
               delete sc;
           }
           catch(DmlException d)
           {   
              
               return 'While deleting Sales Contract'+d;
           }
        }
        Batch__c[] batch = [Select Id from Batch__c b where b.Id = :batchId];
        try
        {
        if(batch.size() > 0)
            delete batch[0];
        }
        catch(DmlException d)
        {   
            
            return 'While deleting Batch  '+d;
        }
        
        return 'Batch Deleted';
    }//end of deleteBatch(....) method
    
    WebService static String insertMergeNames(String[] checkedNames, String templateName, String templateId, String payeeId, String effectiveDate)
    {
        TemplateDataMap__c[] tdm = [Select Name1__c, Name2__c, Name3__c, Name4__c, Name5__c, Name6__c, Name7__c, Name8__c, Name9__c, Name10__c, Name11__c, Name12__c, Name13__c, Name14__c, Name15__c, Name16__c, Name17__c, Name18__c, Name19__c, Name20__c, Name21__c, Name22__c, Name23__c, Name24__c, Name25__c, Name26__c, Name27__c, Name28__c, Name29__c, Name30__c,TemplateName__c, PayeeID__c, EffectiveDate__c from TemplateDataMap__c t where t.TemplateId__c = :templateId];
        if(tdm.size() == 0)        
        {
            tdm = new TemplateDataMap__c[1];
            tdm[0] = new TemplateDataMap__c(TemplateName__c = templateName, TemplateId__c = templateId, PayeeID__c = payeeId, EffectiveDate__c = Date.valueOf(effectiveDate));
        }
        Integer k=0;
        Integer arraySize = checkedNames.size();
        
        tdm[0].PayeeID__c = payeeId;
        
        tdm[0].EffectiveDate__c = Date.valueOf(effectiveDate);
        
        tdm[0].Name1__c = checkedNames[k];
        k++;
        tdm[0].Name2__c = checkedNames[k];
        k++;
        tdm[0].Name3__c = checkedNames[k];
        k++;
        tdm[0].Name4__c = checkedNames[k];
        k++;
        tdm[0].Name5__c = checkedNames[k];
        k++;
        tdm[0].Name6__c = checkedNames[k];
        k++;
        tdm[0].Name7__c = checkedNames[k];
        k++;
        tdm[0].Name8__c = checkedNames[k];
        k++;
        tdm[0].Name9__c = checkedNames[k];
        k++;
            
        if((k+1) <= arraySize) 
        {
            tdm[0].Name10__c = checkedNames[k];
            k++;
        }
        else
        {
            tdm[0].Name10__c = '';
        }
        if((k+1) <= arraySize) 
        {
            tdm[0].Name11__c = checkedNames[k];
            k++;
        }
        else
        {
            tdm[0].Name11__c = '';
        }

        if((k+1) <= arraySize) 
        {
            tdm[0].Name12__c = checkedNames[k];
            k++;
        }
        else
        {
            tdm[0].Name12__c = '';
        }
        if((k+1) <= arraySize) 
        {
            tdm[0].Name13__c = checkedNames[k];
            k++;
        }
        else
        {
            tdm[0].Name13__c = '';
        }
        if((k+1) <= arraySize) 
        {
            tdm[0].Name14__c = checkedNames[k];
            k++;
        }
        else
        {
            tdm[0].Name14__c = '';
        }
        if((k+1) <= arraySize) 
        {
            tdm[0].Name15__c = checkedNames[k];
            k++;
        }
        else
        {
            tdm[0].Name15__c = '';
        }
        if((k+1) <= arraySize) 
        {
            tdm[0].Name16__c = checkedNames[k];
            k++;
        }
        else
        {
            tdm[0].Name16__c = '';
        }
        if((k+1) <= arraySize) 
        {
            tdm[0].Name17__c = checkedNames[k];
            k++;
        }
        else
        {
            tdm[0].Name17__c = '';
        }
        if((k+1) <= arraySize) 
        {
            tdm[0].Name18__c = checkedNames[k];
            k++;
        }
        else
        {
            tdm[0].Name18__c = '';
        }
        if((k+1) <= arraySize) 
        {
            tdm[0].Name19__c = checkedNames[k];
            k++;
        }
        else
        {
            tdm[0].Name19__c = '';
        }
        if((k+1) <= arraySize) 
        {
            tdm[0].Name20__c = checkedNames[k];
            k++;
        }
        else
        {
            tdm[0].Name20__c = '';
        }
        if((k+1) <= arraySize) 
        {
            tdm[0].Name21__c = checkedNames[k];
            k++;
        }
        else
        {
            tdm[0].Name21__c = '';
        }
        if((k+1) <= arraySize) 
        {
            tdm[0].Name22__c = checkedNames[k];
            k++;
        }
        else
        {
            tdm[0].Name22__c = '';
        }
        if((k+1) <= arraySize) 
        {
            tdm[0].Name23__c = checkedNames[k];
            k++;
        }
        else
        {
            tdm[0].Name23__c = '';
        }
        if((k+1) <= arraySize) 
        {
            tdm[0].Name24__c = checkedNames[k];
            k++;
        }
        else
        {
            tdm[0].Name24__c = '';
        }
        if((k+1) <= arraySize) 
        {
            tdm[0].Name25__c = checkedNames[k];
            k++;
        }
        else
        {
            tdm[0].Name25__c = '';
        }
        if((k+1) <= arraySize) 
        {
            tdm[0].Name26__c = checkedNames[k];
            k++;
        }
        else
        {
            tdm[0].Name26__c = '';
        }
        if((k+1) <= arraySize) 
        {
            tdm[0].Name27__c = checkedNames[k];
            k++;
        }
        else
        {
            tdm[0].Name27__c = '';
        }
        if((k+1) <= arraySize) 
        {
            tdm[0].Name28__c = checkedNames[k];
            k++;
        }
        else
        {
            tdm[0].Name28__c = '';
        }
        if((k+1) <= arraySize) 
        {
            tdm[0].Name29__c = checkedNames[k];
            k++;
        }
        else
        {
            tdm[0].Name29__c = '';
        }
        if((k+1) <= arraySize) 
        {
            tdm[0].Name30__c = checkedNames[k];
            k++;
        }
        else
        {
            tdm[0].Name30__c = '';
        }

        
        try
        {
            upsert tdm;
        }
        catch(Exception e)
        {   
            
            return e+'';
        }
        
        return 'inserted successfully.';
    }
    
    
    static testMethod void testPopulateBatchedUsers() 
    {
        String[] userIdList = new String[1];
        String[] roleIdList = new String[1];
        String templateId = 'a0D40000001mHXT';
        String batchId = 'a0J40000000OSsUEAW';
        
        userIdList[0] = '005400000011eqL';
        roleIdList[0] = '00E40000000ju5r';
        
        PopulateBatchedUsers.populateObject(userIdList, roleIdList, templateId, batchId);
        PopulateBatchedUsers.populateObject(userIdList, roleIdList, templateId, 'null');

        PopulateBatchedUsers.populateObject2(userIdList, roleIdList, templateId, batchId);
        PopulateBatchedUsers.populateObject2(userIdList, roleIdList, templateId, 'null');

        PopulateBatchedUsers.getSelectedUsers(batchId, templateId);

        PopulateBatchedUsers.getBatchDetails(templateId);
        
//        System.assertEquals('06/16/2008 10:59:38 PM__06/16/2008 10:59:38 PM', PopulateBatchedUsers.getStartEndDateTime(batchId));
        PopulateBatchedUsers.getStartEndDateTime(batchId);
        
        PopulateBatchedUsers.deleteBatch(batchId);
        String[] mergeNames = new String[9];
        mergeNames[0] = '';
        mergeNames[1] = '';
        mergeNames[2] = '';
        mergeNames[3] = '';
        mergeNames[4] = '';
        mergeNames[5] = '';
        mergeNames[6] = '';
        mergeNames[7] = '';
        mergeNames[8] = '';
                
        insertMergeNames(mergeNames, 'Test Jul 2','a0D40000001mHXT','' ,'2008-06-25');

        mergeNames = new String[30];
        mergeNames[0] = '';
        mergeNames[1] = '';
        mergeNames[2] = '';
        mergeNames[3] = '';
        mergeNames[4] = '';
        mergeNames[5] = '';
        mergeNames[6] = '';
        mergeNames[7] = '';
        mergeNames[8] = '';
        mergeNames[9] = '';
        mergeNames[10] = '';
        mergeNames[11] = '';
        mergeNames[12] = '';
        mergeNames[13] = '';
        mergeNames[14] = '';
        mergeNames[15] = '';
        mergeNames[16] = '';
        mergeNames[17] = '';
        mergeNames[18] = '';
        mergeNames[19] = '';
        mergeNames[20] = '';
        mergeNames[21] = '';
        mergeNames[22] = '';
        mergeNames[23] = '';
        mergeNames[24] = '';
        mergeNames[25] = '';
        mergeNames[26] = '';
        mergeNames[27] = '';
        mergeNames[28] = '';
        mergeNames[29] = '';
                
        insertMergeNames(mergeNames, 'Test Jul 2','a0D40000001mHXT','' ,'2008-06-25');
        populateRemoteAddress('');
    }
}